home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / lib / perl5 / HTML / HeadParser.pm < prev    next >
Text File  |  2009-02-09  |  8KB  |  300 lines

  1. package HTML::HeadParser;
  2.  
  3. =head1 NAME
  4.  
  5. HTML::HeadParser - Parse <HEAD> section of a HTML document
  6.  
  7. =head1 SYNOPSIS
  8.  
  9.  require HTML::HeadParser;
  10.  $p = HTML::HeadParser->new;
  11.  $p->parse($text) and  print "not finished";
  12.  
  13.  $p->header('Title')          # to access <title>....</title>
  14.  $p->header('Content-Base')   # to access <base href="http://...">
  15.  $p->header('Foo')            # to access <meta http-equiv="Foo" content="...">
  16.  $p->header('X-Meta-Author')  # to access <meta name="author" content="...">
  17.  $p->header('X-Meta-Charset') # to access <meta charset="...">
  18.  
  19. =head1 DESCRIPTION
  20.  
  21. The C<HTML::HeadParser> is a specialized (and lightweight)
  22. C<HTML::Parser> that will only parse the E<lt>HEAD>...E<lt>/HEAD>
  23. section of an HTML document.  The parse() method
  24. will return a FALSE value as soon as some E<lt>BODY> element or body
  25. text are found, and should not be called again after this.
  26.  
  27. Note that the C<HTML::HeadParser> might get confused if raw undecoded
  28. UTF-8 is passed to the parse() method.  Make sure the strings are
  29. properly decoded before passing them on.
  30.  
  31. The C<HTML::HeadParser> keeps a reference to a header object, and the
  32. parser will update this header object as the various elements of the
  33. E<lt>HEAD> section of the HTML document are recognized.  The following
  34. header fields are affected:
  35.  
  36. =over 4
  37.  
  38. =item Content-Base:
  39.  
  40. The I<Content-Base> header is initialized from the E<lt>base
  41. href="..."> element.
  42.  
  43. =item Title:
  44.  
  45. The I<Title> header is initialized from the E<lt>title>...E<lt>/title>
  46. element.
  47.  
  48. =item Isindex:
  49.  
  50. The I<Isindex> header will be added if there is a E<lt>isindex>
  51. element in the E<lt>head>.  The header value is initialized from the
  52. I<prompt> attribute if it is present.  If no I<prompt> attribute is
  53. given it will have '?' as the value.
  54.  
  55. =item X-Meta-Foo:
  56.  
  57. All E<lt>meta> elements containing a C<name> attribute will result in
  58. headers using the prefix C<X-Meta-> appended with the value of the
  59. C<name> attribute as the name of the header, and the value of the
  60. C<content> attribute as the pushed header value.
  61.  
  62. E<lt>meta> elements containing a C<http-equiv> attribute will result
  63. in headers as in above, but without the C<X-Meta-> prefix in the
  64. header name.
  65.  
  66. E<lt>meta> elements containing a C<charset> attribute will result in
  67. an C<X-Meta-Charset> header, using the value of the C<charset>
  68. attribute as the pushed header value.
  69.  
  70. =back
  71.  
  72. =head1 METHODS
  73.  
  74. The following methods (in addition to those provided by the
  75. superclass) are available:
  76.  
  77. =over 4
  78.  
  79. =cut
  80.  
  81.  
  82. require HTML::Parser;
  83. @ISA = qw(HTML::Parser);
  84.  
  85. use HTML::Entities ();
  86.  
  87. use strict;
  88. use vars qw($VERSION $DEBUG);
  89. #$DEBUG = 1;
  90. $VERSION = "3.60";
  91.  
  92. =item $hp = HTML::HeadParser->new
  93.  
  94. =item $hp = HTML::HeadParser->new( $header )
  95.  
  96. The object constructor.  The optional $header argument should be a
  97. reference to an object that implement the header() and push_header()
  98. methods as defined by the C<HTTP::Headers> class.  Normally it will be
  99. of some class that is a or delegates to the C<HTTP::Headers> class.
  100.  
  101. If no $header is given C<HTML::HeadParser> will create an
  102. C<HTTP::Header> object by itself (initially empty).
  103.  
  104. =cut
  105.  
  106. sub new
  107. {
  108.     my($class, $header) = @_;
  109.     unless ($header) {
  110.     require HTTP::Headers;
  111.     $header = HTTP::Headers->new;
  112.     }
  113.  
  114.     my $self = $class->SUPER::new(api_version => 2,
  115.                   ignore_elements => [qw(script style)],
  116.                  );
  117.     $self->{'header'} = $header;
  118.     $self->{'tag'} = '';   # name of active element that takes textual content
  119.     $self->{'text'} = '';  # the accumulated text associated with the element
  120.     $self;
  121. }
  122.  
  123. =item $hp->header;
  124.  
  125. Returns a reference to the header object.
  126.  
  127. =item $hp->header( $key )
  128.  
  129. Returns a header value.  It is just a shorter way to write
  130. C<$hp-E<gt>header-E<gt>header($key)>.
  131.  
  132. =cut
  133.  
  134. sub header
  135. {
  136.     my $self = shift;
  137.     return $self->{'header'} unless @_;
  138.     $self->{'header'}->header(@_);
  139. }
  140.  
  141. sub as_string    # legacy
  142. {
  143.     my $self = shift;
  144.     $self->{'header'}->as_string;
  145. }
  146.  
  147. sub flush_text   # internal
  148. {
  149.     my $self = shift;
  150.     my $tag  = $self->{'tag'};
  151.     my $text = $self->{'text'};
  152.     $text =~ s/^\s+//;
  153.     $text =~ s/\s+$//;
  154.     $text =~ s/\s+/ /g;
  155.     print "FLUSH $tag => '$text'\n"  if $DEBUG;
  156.     if ($tag eq 'title') {
  157.     HTML::Entities::decode($text);
  158.     $self->{'header'}->push_header(Title => $text);
  159.     }
  160.     $self->{'tag'} = $self->{'text'} = '';
  161. }
  162.  
  163. # This is an quote from the HTML3.2 DTD which shows which elements
  164. # that might be present in a <HEAD>...</HEAD>.  Also note that the
  165. # <HEAD> tags themselves might be missing:
  166. #
  167. # <!ENTITY % head.content "TITLE & ISINDEX? & BASE? & STYLE? &
  168. #                            SCRIPT* & META* & LINK*">
  169. #
  170. # <!ELEMENT HEAD O O  (%head.content)>
  171. #
  172. # From HTML 4.01:
  173. #
  174. # <!ENTITY % head.misc "SCRIPT|STYLE|META|LINK|OBJECT">
  175. # <!ENTITY % head.content "TITLE & BASE?">
  176. # <!ELEMENT HEAD O O (%head.content;) +(%head.misc;)>
  177. #
  178. # Added in HTML 5: noscript, eventsource, command
  179.  
  180. sub start
  181. {
  182.     my($self, $tag, $attr) = @_;  # $attr is reference to a HASH
  183.     print "START[$tag]\n" if $DEBUG;
  184.     $self->flush_text if $self->{'tag'};
  185.     if ($tag eq 'meta') {
  186.     my $key = $attr->{'http-equiv'};
  187.     if (!defined($key) || !length($key)) {
  188.         if ($attr->{name}) {
  189.         $key = "X-Meta-\u$attr->{name}";
  190.         } elsif ($attr->{charset}) { # HTML 5 <meta charset="...">
  191.         $key = "X-Meta-Charset";
  192.         $self->{header}->push_header($key => $attr->{charset});
  193.         return;
  194.         } else {
  195.         return;
  196.         }
  197.     }
  198.     $self->{'header'}->push_header($key => $attr->{content});
  199.     } elsif ($tag eq 'base') {
  200.     return unless exists $attr->{href};
  201.     $self->{'header'}->push_header('Content-Base' => $attr->{href});
  202.     } elsif ($tag eq 'isindex') {
  203.     # This is a non-standard header.  Perhaps we should just ignore
  204.     # this element
  205.     $self->{'header'}->push_header(Isindex => $attr->{prompt} || '?');
  206.     } elsif ($tag =~ /^(?:title|(?:no)?script|style|object
  207.               |eventsource|command)$/x) {
  208.     # Just remember tag.  Initialize header when we see the end tag.
  209.     $self->{'tag'} = $tag;
  210.     } elsif ($tag eq 'link') {
  211.     return unless exists $attr->{href};
  212.     # <link href="http:..." rel="xxx" rev="xxx" title="xxx">
  213.     my $h_val = "<" . delete($attr->{href}) . ">";
  214.     for (sort keys %{$attr}) {
  215.         next if $_ eq "/";  # XHTML junk
  216.         $h_val .= qq(; $_="$attr->{$_}");
  217.     }
  218.     $self->{'header'}->push_header(Link => $h_val);
  219.     } elsif ($tag eq 'head' || $tag eq 'html') {
  220.     # ignore
  221.     } else {
  222.      # stop parsing
  223.     $self->eof;
  224.     }
  225. }
  226.  
  227. sub end
  228. {
  229.     my($self, $tag) = @_;
  230.     print "END[$tag]\n" if $DEBUG;
  231.     $self->flush_text if $self->{'tag'};
  232.     $self->eof if $tag eq 'head';
  233. }
  234.  
  235. sub text
  236. {
  237.     my($self, $text) = @_;
  238.     print "TEXT[$text]\n" if $DEBUG;
  239.     unless ($self->{first_chunk}) {
  240.     # drop Unicode BOM if found
  241.     if ($self->utf8_mode) {
  242.         $text =~ s/^\xEF\xBB\xBF//;
  243.     }
  244.     else {
  245.         $text =~ s/^\x{FEFF}//;
  246.     }
  247.     $self->{first_chunk}++;
  248.     }
  249.     my $tag = $self->{tag};
  250.     if (!$tag && $text =~ /\S/) {
  251.     # Normal text means start of body
  252.         $self->eof;
  253.     return;
  254.     }
  255.     return if $tag ne 'title';
  256.     $self->{'text'} .= $text;
  257. }
  258.  
  259. BEGIN {
  260.     *utf8_mode = sub { 1 } unless HTML::Entities::UNICODE_SUPPORT;;
  261. }
  262.  
  263. 1;
  264.  
  265. __END__
  266.  
  267. =back
  268.  
  269. =head1 EXAMPLE
  270.  
  271.  $h = HTTP::Headers->new;
  272.  $p = HTML::HeadParser->new($h);
  273.  $p->parse(<<EOT);
  274.  <title>Stupid example</title>
  275.  <base href="http://www.linpro.no/lwp/">
  276.  Normal text starts here.
  277.  EOT
  278.  undef $p;
  279.  print $h->title;   # should print "Stupid example"
  280.  
  281. =head1 SEE ALSO
  282.  
  283. L<HTML::Parser>, L<HTTP::Headers>
  284.  
  285. The C<HTTP::Headers> class is distributed as part of the
  286. I<libwww-perl> package.  If you don't have that distribution installed
  287. you need to provide the $header argument to the C<HTML::HeadParser>
  288. constructor with your own object that implements the documented
  289. protocol.
  290.  
  291. =head1 COPYRIGHT
  292.  
  293. Copyright 1996-2001 Gisle Aas. All rights reserved.
  294.  
  295. This library is free software; you can redistribute it and/or
  296. modify it under the same terms as Perl itself.
  297.  
  298. =cut
  299.  
  300.